<?php

/**
 * Created by PhpStorm.
 * User: Jackmao
 * Date: 2017/8/10
 * Time: 17:08
 */

use \LeanCloud\User;

class Manager extends BaseController
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("Manager_model", "manager_model");
        $this->load->helper("acl");
    }

    public function login()
    {
        if (!acl()) {
            $this->load->view("manager/login");
        } else {
            redirect("../dashbord/index");
        }
    }

    public function logout()
    {
        User::logout();
        redirect("../manager/login");
    }

    public function verify()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        if ($this->manager_model->verify($username, $password)) {
            if (!acl()) {
                $this->echo_json('您没有相应权限', false);
                return;
            }
            $this->echo_json('登录成功');
        } else {
            $this->echo_json('用户名或密码错误', false);
        }
    }

    public function updatePassword()
    {
        $oldPassword = $this->input->post('oldPassword');
        $newPassword = $this->input->post('newPassword');
        try {
            User::getCurrentUser()->updatePassword($oldPassword, $newPassword);
            $this->echo_json('密码修改成功', true);
        } catch (Exception $e) {
            switch ($e->getCode()) {
                case 1:
                    $message = '密码不能为空';
                    break;
                case 210:
                    $message = '旧密码错误';
                    break;
                default:
                    $message = '密码修改失败' . $e->getCode();
                    break;
            }
            $this->echo_json($message, false);
        }
    }

    public function profile()
    {
        $this->layout->view('manager/profile');
    }

    public function save()
    {
        $data = array(
            'msg' => '操作成功',
            'level' => 'info',
            'redirect' => 'profile'
        );
        $this->layout->view('manager/msg', $data);
    }
}